package com.ai.web.aop;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Service;


@Service
public class LogAdvice {

	private static Logger logger = Logger.getLogger(LogAdvice.class);
	
	
	public void logBefore(JoinPoint point) {
		
		Method method = ((MethodSignature)point.getSignature()).getMethod();
		
		logger.info("[LogAdvice logBefore] target:" + method.getDeclaringClass().getName() + " | method:" + method.getName() );
		
	}
	
	public void logAfterAndReturn(JoinPoint point, Object returnObj) {
		
		Method method = ((MethodSignature)point.getSignature()).getMethod();
		
		logger.info("[LogAdvice logAfterAndReturn] target:" + method.getDeclaringClass().getName() + " | method:" + method.getName() );
		
	}
	
	public Object logAround(ProceedingJoinPoint point) throws Throwable {
		
		long start = System.currentTimeMillis();
		
		Object obj = point.proceed();
		
		long end = System.currentTimeMillis();
		
		Method method = ((MethodSignature)point.getSignature()).getMethod();
		logger.info("[LogAdvice logAround] target:" + method.getDeclaringClass().getName() + " | method:" + method.getName() + " | cost:" + (end - start) + "ms.");
		
		return obj;
	}
}
